#!/bin/sh

# 工作路径
g_workPath=$(cd `dirname \$0`; pwd -P)
cd $g_workPath

. $g_workPath/script/func.sh
. $g_workPath/.env

help() {
	cat << EOF
用法: ./ctrl [选项]
可用选项:
    init            初始化容器部署文件(端口转发), 没有执行 private 之前
    init private    初始化容器部署文件(nginx 代理)
    init public     还原容器部署文件为(端口转发)
EOF
}

init() {
	# 创建卷
	if [ "$1" == "private" ]
	then 
		setEnv "network" "net-private" $g_workPath/.env \
		&& sed -i "s/^        # ipv4_address/        ipv4_address/" docker-compose.yaml \
		&& sed -i "s/172.18.254.197/172.19.254.197/" docker-compose.yaml \
		&& sed -i "s/172.18.254.197/172.19.254.197/" joplin_server.nginx.conf \
		&& succEcho "convert private success"
	elif [ "$1" == "public" ]
	then
		setEnv "network" "net-public" $g_workPath/.env \
		&& sed -i "s/^        # ipv4_address/        ipv4_address/" docker-compose.yaml \
		&& sed -i "s/172.19.254.197/172.18.254.197/" docker-compose.yaml \
		&& sed -i "s/172.19.254.197/172.18.254.197/" joplin_server.nginx.conf \
		&& succEcho "convert public success"
	fi
}

case $1 in
	init)
		. ./script/init.sh
		init "$2"
		;;
	help)
		help
		;;
esac	
